<?
$host = 'endeepen.com';
$user = 'tpb';
$password = '';
$dbname = 'endeepen';


function insertNewInvite($email_address, 
						 $ticket=NULL, 
						 $redeemed = NULL,
						 $webuser_id=NULL, 
						 $subdomain=NULL,
						 $cname = NULL,
						 $created = NULL) 
{
	
	if ($email_address === NULL)
		return NULL;
	
	$fields_list_str = 'email_address';
	$values_list_str = "'" . $email_address . "'";
	
	if ($ticket) {
		$fields_list_str .= ', ticket';
		$values_list_str .= ', ' . "'" . (string)$ticket . "'";
	}
	if ($webuser_id) {
		$fields_list_str .= ', webuser_id';
		$values_list_str .= ', ' . "'" . (string)$webuser_id . "'";
	}
	if ($subdomain) {
		$fields_list_str .= ', subdomain';
		$values_list_str .= ', ' . "'" . (string)$subdomain . "'";
	}
	if ($cname) {
		$fields_list_str .= ', cname';
		$values_list_str .= ', ' . "'" . (string)$cname . "'";
	}
	if ($redeemed) {
		$fields_list_str .= ', redeemed';
		$values_list_str .= ', ' . "'" . (string)$redeemed . "'";
	}
	if ($created) {
		$fields_list_str .= ', created';
		$values_list_str .= ', ' . "'" . (string)$created . "'";
	}
	
	$sql = "insert into instances($fields_list_str) values($values_list_str)";
	$result = executeUpdateSQL($sql);
	return $result;
}

function updateInviteByTicket($ticket=NULL, 
						$email_address=NULL, 
						$redeemed = NULL,
						$webuser_id=NULL, 
						$subdomain=NULL,
						$cname = NULL,
						$created = NULL) 
{
	if ($ticket === NULL)
		return NULL;

	$set_str = "ticket='$ticket'";
	
	if ($email_address) {
		$set_str .= ', email_address=' . "'" . $email_address . "'";
	}
	if ($redeemed) {
		$set_str .= ', redeemed=' . "'" . $redeemed . "'";
	}
	if ($webuser_id) {
		$set_str .= ', webuser_id=' . "'" . $webuser_id . "'";
	}
	if ($subdomain) {
		$set_str .= ', subdomain=' . "'" . $subdomain . "'";
	}
	if ($cname) {
		$set_str .= ', cname=' . "'" . $cname . "'";
	}
	if ($created) {
		$set_str .= ', created=' . "'" . $created . "'";
	}
	
	$sql = "update instances set $set_str  where ticket = '$ticket'"; 						 	
	$result = executeUpdateSQL($sql);
	return $result;
						 
}


function updateInstanceByEmailAddress(
						$email_address=NULL,
						$subdomain=NULL,
						$cname = NULL,
						$webuser_id=NULL, 
						$ticket=NULL, 
						$redeemed = NULL
						) 
{
	if ($email_address === NULL)
		return NULL;

	$set_str = "email_address='$email_address'";
	
	if ($subdomain) {
		$set_str .= ', subdomain=' .  "'" . $subdomain . "'";
	}
	if ($cname) {
		$set_str .= ', cname=' .  "'" . $cname . "'";
	}
	if ($webuser_id) {
		$set_str .= ', webuser_id=' .  "'" . $webuser_id . "'";
	}
	if ($ticket) {
		$set_str .= ', ticket=' .  "'" . $ticket . "'";
	}
	if ($redeemed) {
		$set_str .= ', redeemed=' .  "'" . $redeemed . "'";
	}
	
	$sql = "update set <$set_str>  where email_address = '$email_address'"; 						 	
	$result = executeUpdateSQL($sql);
	return $result;
						 
}


function connectToDB() {
    global $host, $user, $password, $dbname;
    $link = mysql_connect($host, $user) or die('Could not connect: ' . mysql_error());
    mysql_select_db($dbname) or die('Could not select database');
    return $link;
}

function execute($query) {
    #print '<br><b>query</b>=' . $query;
    $link = (isset($link))? $link: connectToDB() ;
    $rs = mysql_query($query) or die('Query failed: ' . mysql_error());
    $result = array();
    while ($row = mysql_fetch_assoc($rs)) {
        $result[] = $row;
    }
    mysql_close($link);
    mysql_free_result($rs);
    return $result;
}

function executeUpdateSQL($query) {
    #print '<br><b>query</b>=' . $query . '<br><br>';
    $link = connectToDB() ;
    $rs = mysql_query($query); 
    if(!$rs) throw new Exception(mysql_error()); // or die('Query failed: ' . mysql_error());
    mysql_close($link);
    return true;
}




?>